<%@ Page language="c#" debug="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%@ Import Namespace="Jayrock.Json" %>
<%@ Import Namespace="Jayrock.Json.Conversion" %>

<!-- #Include File="classes/query.aspx -->
<!-- #Include File="classes/command.aspx -->

<%
    InitializeWebTier();

    NameValueCollection args = GetRequestParameters();
    Query query = new Query(args);
    String responseType = "";
    String responseString = "";

    if(GetParameter(args, "COMMAND") != null)
    {
        int cmd = Int32.Parse(GetParameter(args, "COMMAND"));
        switch (cmd)
        {
            case Command.GETLAYERPROPERTIES:
                responseType = "text/plain";
                ArrayList properties = query.GetLayerProperties();
                responseString = JsonConvert.ExportToString(properties.ToArray());
                break;
            case Command.TOGGLESPATIALFILTER:
                responseType = "text/plain";
                responseString = JsonConvert.ExportToString(query.ToggleSpatialFilter());
                break;
            case Command.SHOWSPATIALFILTER:
                responseType = "text/plain";
                responseString = JsonConvert.ExportToString(query.ShowSpatialFilter());
                break;
            case Command.EXECUTEQUERY:
                responseType = "text/plain";
                ArrayList resultList = query.Execute();
                responseString = JsonConvert.ExportToString(resultList.ToArray());
                break;
            case Command.GETSELECTIONXML:
                responseType = "text/xml";
                responseString = query.GetSelectionXML();
                break;
        }
    }
    Response.ContentType = responseType;
    Response.Write(responseString);
    //throw(null);
%>